/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cannes.dao.implement;

import cannes.bean.planning.Invitation;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
 *
 * @author Pierre
 */
public class InvitationDAO extends DAO<Invitation> {
    
        public InvitationDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Invitation obj) {
        
        String req = "INSERT INTO invitation VALUES ('"+obj.getId()+"',"
                + "'"+obj.getProjectionId()+"','"+obj.getVipId()+"','"+obj.getPlace()+"')";
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(req);
            return result.rowInserted();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }

    @Override
    public boolean update(Invitation obj) {
        try
        {
        this.connect.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(
                "UPDATE invitation SET projectionid =" + obj.getProjectionId() +",vipid = " + obj.getVipId() + ",place" + obj.getPlace() + "WHERE invitationid = " + obj.getId());
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return false;
    }

    @Override
    public boolean delete(Invitation obj) {
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "DELETE FROM invitation WHERE invitationid = " + obj.getId());
            return result.rowDeleted();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }

    @Override
    public int next() {
        int i = 0;
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT max(invitationid) from invitation");
            if (result.first()) i = result.getInt(1)+1;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }



    @Override
    public Invitation find(int id) {
        Invitation invit = new Invitation();

        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM invitation WHERE invitationid = " + id);
            if (result.first()) {
                invit = new Invitation(result.getInt("invitationid"), result.getInt("vipid"),
                        result.getInt("projectionid"), result.getInt("place"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return invit;

    }

    @Override
    public ArrayList<Invitation> findAll() {
        ArrayList<Invitation> listeInvit = new ArrayList<Invitation>();
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM invitation");
            while(result.next()){
                listeInvit.add(new Invitation(result.getInt("invitationid"), result.getInt("vipid"),
                        result.getInt("projectionid"), result.getInt("place")));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return listeInvit;
    }
    
    public ArrayList<Invitation> findByVipId(int id) {
        ArrayList<Invitation> listeInvit = new ArrayList<Invitation>();
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM invitation WHERE vipid = "+id);
            while(result.next()){
                listeInvit.add(new Invitation(result.getInt("invitationid"), result.getInt("vipid"),
                        result.getInt("projectionid"), result.getInt("place")));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return listeInvit;
    }
    
        public ArrayList<Invitation> findByProjectionId(int id) {
        ArrayList<Invitation> listeInvit = new ArrayList<Invitation>();
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM film WHERE projectionid = "+id);
            while(result.next()){
                listeInvit.add(new Invitation(result.getInt("invitationid"), result.getInt("vipid"),
                        result.getInt("projectionid"), result.getInt("place")));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return listeInvit;
    }
    
}
